package com.caocao.service.impl;

import com.caocao.domain.AccountConst;
import com.caocao.domain.mapper.TCodedictMapper;
import com.caocao.domain.mapper.TUserMapper;
import com.caocao.domain.model.TCodedict;
import com.caocao.domain.model.TCodedictExample;
import com.caocao.domain.model.TUser;
import com.caocao.domain.model.TUserExample;
import com.caocao.service.CommonService;
import com.caocao.utils.VerifyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述:公共接口的实现类
 *
 * @Author:操文健
 * @Create Time:2017-09-15 20:29
 */
@Service
public class CommonServiceImpl implements CommonService {

    private Logger logger = LoggerFactory.getLogger(CommonServiceImpl.class);

    @Autowired
    private TUserMapper tUserMapper;

    @Autowired
    private TCodedictMapper tCodedictMapper;


    @Override
    public List<Map<String, Object>> getCodeForName(String codeType) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        // 非空性判断
        if (VerifyUtil.isEmptyString(codeType)) {
            logger.info("查询CodeType为空...");
            return null;
        }
        // 用户返回结果集使用
        // 判断查询的类型是用户还是数据字段中的数据值
        if (AccountConst.CODETYPE_USER.equals(codeType)) {
            TUserExample tUserExample = new TUserExample();
            tUserExample.createCriteria().andIsvalidEqualTo(AccountConst.FLAG_TEUE);
            tUserExample.setOrderByClause("id");
            List<TUser> userList = tUserMapper.selectByExample(tUserExample);
            if (VerifyUtil.isEmpty(userList)) {
                logger.info("查询用户信息为空...");
                return null;
            }
            for (TUser tUser : userList) {
                Map<String, Object> resultMap = new HashMap<String, Object>();
                resultMap.put("code", tUser.getId());
                resultMap.put("name", tUser.getUsername());
                result.add(resultMap);
            }
        } else {
            TCodedictExample tCodedictExample = new TCodedictExample();
            tCodedictExample.createCriteria().andCodetypeEqualTo(codeType);
            List<TCodedict> codedictList = tCodedictMapper.selectByExample(tCodedictExample);
            if (VerifyUtil.isEmpty(codedictList)) {
                logger.info("数据字段根据类型查询数据为空....");
                return null;
            }
            for (TCodedict codedict : codedictList) {
                Map<String, Object> resultMap = new HashMap<String, Object>();
                resultMap.put("code", codedict.getCode());
                resultMap.put("name", codedict.getCodename());
                result.add(resultMap);
            }
        }
        return result;
    }
}
